﻿using BCDto.WeChatComponent.ErrorMessage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using WeChatSDK.Common;
using WeChatSDK.WeChatDto.Request.Http;

namespace BCService.WeChatComponent
{
    /// <summary>
    /// 微信HttpResponse处理
    /// </summary>
    public class WeChatHttpResponseHandle
    {
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="result">返回的消息</param>
        /// <returns></returns>
        public static string ProcessMessage(HttpResponseDto result)
        {
            if (result.IsError)
            {
                if (result.ResponseMessage.IndexOf("errcode") > -1)
                {
                    WeChatErrorMessageDto weChatErrorMessageDto = JsonConvert.DeserializeObject<WeChatErrorMessageDto>(result.ResponseMessage, WeChatGeneral.JsonSerializerSettings);
                    throw new ArgumentException(weChatErrorMessageDto.ErrMsg);
                }
                throw new ArgumentException(result.ResponseMessage);
            }
            return result.ResponseMessage;
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="result">返回的消息</param>
        /// <returns></returns>
        public static byte[] ProcessData(HttpResponseDto result)
        {
            if (result.IsError)
            {
                if (result.ResponseMessage.IndexOf("errcode") > -1)
                {
                    WeChatErrorMessageDto weChatErrorMessageDto = JsonConvert.DeserializeObject<WeChatErrorMessageDto>(result.ResponseMessage, WeChatGeneral.JsonSerializerSettings);
                    throw new ArgumentException(weChatErrorMessageDto.ErrMsg);
                }
                throw new ArgumentException(result.ResponseMessage);
            }
            return result.ResponseData;
        }
    }
}
